# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: api\handlers\scan\scans.py
__author__ = "sanyi"
import logging
from api.classes.scan.auto import *
from api.handlers.scan import ScanApiHandler
from server.http_responses.http_errors import Http409, Http404
from server.http_responses.http_status_codes import Http204
from helpers.setup_types import is_setup_type_on_premise_worker
from settings_provider import settings
from helpers.address import change_url_address
from helpers.licensing.features import Features

_logger = logging.getLogger("api.handlers.scans")


class ScansApiHandler(ScanApiHandler):
    def start_job(self, body, **_):
        job_description = ClsJobDescription(**body)
        if is_setup_type_on_premise_worker():
            self.license.require(Features.MULTI_ENGINE)
            settings_storage = settings.get("worker_settings_storage")
            master_endpoint = settings_storage.get("master_endpoint")
            if not master_endpoint:
                raise Http409("worker is not registered")
            if settings_storage.get("authorization") != "authorized":
                raise Http409("worker is not authorized")
            job_description.status_api_url = change_url_address(
                job_description.status_api_url, master_endpoint
            )
        return self.app.start_job(job_description)

    def abort_job(self, body, **_):
        return self.app.abort_job(ClsJobIdentifier(**body))

    def pause_job(self, body, **_):
        return self.app.pause_job(ClsJobIdentifier(**body))

    def delete_job(self, body, **_):
        self.app.delete_job(ClsJobIdentifier(**body))
        return Http204()

    def get_job_status(self, body, **_):
        return self.app.get_job_status(ClsJobIdentifierList(**body))

    def get_scan_state_db(self, scanning_app, job_id, **_):
        get_scan_state_db = getattr(self.app, "get_scan_state_db")
        if get_scan_state_db is None:
            _logger.warning("get_scan_state_db is not defined")
            raise Http404()
        return get_scan_state_db(scanning_app, job_id)
